﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class CmsSite
    {
        private readonly KeRui.DAL.CmsSite dal = new KeRui.DAL.CmsSite();

        public int Add(KeRui.Model.CmsSite model)
        {
            return this.dal.Add(model);
        }

        public List<KeRui.Model.CmsSite> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.CmsSite> list = new List<KeRui.Model.CmsSite>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.CmsSite item = new KeRui.Model.CmsSite();
                    if (dt.Rows[i]["SiteID"].ToString() != "")
                    {
                        item.SiteID = int.Parse(dt.Rows[i]["SiteID"].ToString());
                    }
                    item.SiteName = dt.Rows[i]["SiteName"].ToString();
                    item.SiteAlias = dt.Rows[i]["SiteAlias"].ToString();
                    item.SiteKey = dt.Rows[i]["SiteKey"].ToString();
                    item.SiteDomain = dt.Rows[i]["SiteDomain"].ToString();
                    item.SiteUrl = dt.Rows[i]["SiteUrl"].ToString();
                    item.SiteDir = dt.Rows[i]["SiteDir"].ToString();
                    if (dt.Rows[i]["TemplateID"].ToString() != "")
                    {
                        item.TemplateID = int.Parse(dt.Rows[i]["TemplateID"].ToString());
                    }
                    item.TemplateName = dt.Rows[i]["TemplateName"].ToString();
                    item.TemplateFolder = dt.Rows[i]["TemplateFolder"].ToString();
                    item.TemplateDir = dt.Rows[i]["TemplateDir"].ToString();
                    item.NodeDefaultTemplateFile = dt.Rows[i]["NodeDefaultTemplateFile"].ToString();
                    item.Domain = dt.Rows[i]["Domain"].ToString();
                    if (dt.Rows[i]["ServerID"].ToString() != "")
                    {
                        item.ServerID = int.Parse(dt.Rows[i]["ServerID"].ToString());
                    }
                    item.ServerIP = dt.Rows[i]["ServerIP"].ToString();
                    if (dt.Rows[i]["IsUsing"].ToString() != "")
                    {
                        item.IsUsing = int.Parse(dt.Rows[i]["IsUsing"].ToString());
                    }
                    if (dt.Rows[i]["OrderID"].ToString() != "")
                    {
                        item.OrderID = int.Parse(dt.Rows[i]["OrderID"].ToString());
                    }
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public void Delete(int SiteID)
        {
            this.dal.Delete(SiteID);
        }

        public DataSet GetCmsSiteChk()
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetSelect("CmsSite", "", " SiteID ", " SiteID,SiteName");
        }

        public DataSet GetDataSet()
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetSelect("CmsSite", "", "OrderID asc", "siteID ,siteName");
        }

        public List<KeRui.Model.CmsSite> GetList()
        {
            DataSet set = new KeRui.DAL.Common().GetList("CmsSite", -1, -1, string.Empty, "OrderID asc");
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsSite> GetList(int templateID)
        {
            DataSet set = new KeRui.DAL.Common().GetList("CmsSite", -1, -1, "templateID = " + templateID + "", "OrderID asc");
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsSite> GetList(int ServerID, int IsUsing)
        {
            string strWhere = "ServerID=" + ServerID + "";
            if ((IsUsing == 0) || (IsUsing == 1))
            {
                strWhere = strWhere + " and IsUsing=" + IsUsing;
            }
            DataSet set = new KeRui.DAL.Common().GetList("CmsSite", -1, -1, strWhere, "OrderID asc");
            return this.DataTableToList(set.Tables[0]);
        }

        public int GetMaxOrderID()
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetMaxOrderID("CmsSite ", "OrderID");
        }

        public KeRui.Model.CmsSite GetModel(int SiteID)
        {
            return this.dal.GetModel(SiteID);
        }

        public KeRui.Model.CmsSite GetModel(string SiteDomain)
        {
            return this.dal.GetModel(SiteDomain);
        }

        public KeRui.Model.CmsSite GetModelByCache(int SiteID)
        {
            string cacheKey = "CmsSiteModel-" + SiteID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.dal.GetModel(SiteID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.CmsSite) cache;
        }

        public KeRui.Model.CmsSite GetModelByCache(string SiteDomain)
        {
            string cacheKey = "CmsSiteModel-" + SiteDomain;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.dal.GetModel(SiteDomain);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.CmsSite) cache;
        }

        public DataSet GetSel()
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetSelect("CmsServer ", "", " ServerID ", " ServerID,ServerName ");
        }

        public KeRui.Model.CmsSite GetSiteModel(string SiteName)
        {
            return this.dal.GetSiteModel(SiteName);
        }

        public DataSet GetSiteSel(int ServerID)
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetSelect("CmsSite ", " ServerID = " + ServerID + " and IsUsing = 1 ", " SiteID ", " SiteID,SiteName ");
        }

        public int Update(KeRui.Model.CmsSite model)
        {
            return this.dal.Update(model);
        }
    }
}

